<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>代理商管理</title>
	<meta name="decorator" content="default"/>
	<%@include file="/WEB-INF/views/include/treetable.jsp" %>
	<script type="text/javascript">
		$(document).ready(function() {
			var tpl = $("#treeTableTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
			var data = ${fns:toJson(list)}, rootId = "${not empty office.id ? office.id : officeId}";
			addRow("#treeTableList", tpl, data, rootId, true);
			$("#treeTable").treeTable({expandLevel : 5});
			//表单验证
			$("#rechargeForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
			});
			$("#searchForm").validate({
				submitHandler: function(form){
					loading('正在查询，请稍等...');
					form.submit();
				},
			});
		});
		function addRow(list, tpl, data, pid, root){
		
			for (var i=0; i<data.length; i++){
				var row = data[i];
				if ((${fns:jsGetVal('row.parentId')}) == pid){
					$(list).append(Mustache.render(tpl, {
						dict: {
							type: getDictLabel(${fns:toJson(fns:getDictList('sys_office_type'))}, row.type)
						}, pid: (root?0:pid), row: row
					}));
					addRow(list, tpl, data, row.id);
				}
			}
		}
		
		//分配套餐
		function batchDistribution(office_id){
			var str = '';
			$.ajax({
				url : "${ctx}/sys/office/packageList?office_id="+office_id,
				type : "GET",
				success : function(data) {
					var taocanId = $("#taocanId");
					  for(var o in data) {
							str += '<option value="'+data[o].id+'">'+data[o].tcId+'_'+data[o].tctype+'_'+data[o].type+'</option>';
						}
					  taocanId.html('<option value="" selected = "selected">--请选择--</option>'+str);
				}
			});
			$("#office_id").val(office_id);//添加需要分配的id
			$('#myModal').modal("show");//显示模态框
		}
		
		function page(){
			loading('查询中，请稍等...');
			$("#searchForm").attr("action","${ctx}/sys/office/list");
			$("#searchForm").submit();
        	return false;
		}
	</script>
</head>
<body>
	<!-- 分配套餐DIV -->
	<form:form id="rechargeForm" modelAttribute="office" action="${ctx}/sys/office/distributio" method="post" class="form-horizontal">
	<div id="myModal" style="width:400px;" class="modal hide fade" tabindex="-1" role="dialog"  aria-hidden="false">
	  <div class="modal-header">
	    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
	    <h3 id="myModalLabel">分配套餐</h3>
	  </div>
		<div class="controls" style="margin-left: 5px;margin-top:10px;margin-bottom: 10px;">
			<input type="hidden" id="office_id" name="office_id" value=""/>
			套餐:
			<select id="taocanId" name="taocanId" class="input-medium required" style="width:200px;">
			</select>
		</div>		
		<div class="controls" style="margin-left: 5px;margin-top:10px;margin-bottom: 10px;">
			<input type="hidden" id="office_id" name="office_id" value=""/>
			价格:
			<form:input path="fee" style="width:200px;" digits="true" htmlEscape="false"  maxlength="10" minlength="1" class="input-xlarge required"/>
		</div>
			<shiro:hasPermission name="recharge:package:list">
				<a style="margin-left: 20px;" href="${ctx}/recharge/package/list">没有套餐？点击添加</a>
			</shiro:hasPermission>
	  <div class="modal-footer">
	    <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>&nbsp;
	    <input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>
	  </div>
	</div>
	</form:form>
	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/sys/office/list">代理商列表</a></li>
		</ul>
 		<form class="breadcrumb form-search" id="searchForm" method="post">
		<ul class="ul-form">
			<li style="margin-left:20px;"><label>代理商名称：</label>
				<input id="name" name="name" htmlEscape="false" value="${office.name}" maxlength="10" class="input-medium"/>
			</li>
			<li style="margin-left:20px !important;" class="btns">
				<input id="btnSubmit" class="btn btn-primary" type="submit" value="查 询"/>
		</ul>
	</form>
	
	<sys:message content="${message}"/>
	<table id="treeTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr><th>代理商/个人名称</th><th>企业注册号</th>
			<th>银行卡号</th>
			<th>企业法人</th>
			<th>企业电话</th>
			<th>手续费</th>
			<shiro:hasPermission name="sys:office:edit">
				<th>操作</th>
			</shiro:hasPermission>
			</tr>
			</thead>
		<tbody id="treeTableList"></tbody>
	</table>
	
	<script type="text/template" id="treeTableTpl">
		<tr id="{{row.id}}" pId="{{pid}}">
			<td>{{row.name}}</td>
			<td>{{row.bussregNo}}</td>
			<td>{{row.cardNo}}</td>
			<td>{{row.contactsName}}</td>
			<td>{{row.phone}}</td>
			<td>{{row.fee}}</td>
			<td>
				<shiro:hasPermission name="sys:office:edit">
					<a href="${ctx}/sys/office/form?id={{row.id}}&types=2">修改</a>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:office:delete">
					<a href="${ctx}/sys/office/delete?id={{row.id}}" onclick="return confirmx('要删除该机构及所有子机构项吗？', this.href)">删除</a>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:office:save">
					<a href="${ctx}/sys/office/form?parent.id={{row.id}}&types=1">添加下级代理商</a>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:office:distribution">
					<a style="cursor:pointer" onclick="batchDistribution('{{row.id}}')">分配</a>
				</shiro:hasPermission>
			</td>
		</tr>
	</script>
</body>
</html>